package com.example.smartall.ui;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.smartall.R;
import com.example.smartall.api.Api;
import com.example.smartall.api.ApiConfig;
import com.example.smartall.api.NetCallBack;
import com.example.smartall.diaglog.LoadedDialog;
import com.example.smartall.entity.JobQueryResumeBean;
import com.example.smartall.entity.JobResumeBean;
import com.example.smartall.ui.base.BaseActivity;
import com.example.smartall.utils.Constant;
import com.google.gson.Gson;

/**
 * 个人简历
 */
public class JobResumeActivity extends BaseActivity {

    private JobQueryResumeBean.DataBean queryResumeBean;

    private ImageView imBack;
    private TextView tvTitle;
    private EditText etMostEducation;
    private EditText etEducation;
    private EditText etAddress;
    private EditText etExperience;
    private EditText etIndividualResume;
    private EditText etPositionId;
    private EditText etMoney;
    private Button btOk;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_job_resume);

        try {
            queryResumeBean = (JobQueryResumeBean.DataBean) getIntent().getSerializableExtra("data");
        }catch (Exception e){
            Log.e(Constant.TAG,"DATA IS NULL");
        }

        init();
        initEvent();
    }

    @Override
    public void init() {
        imBack = (ImageView) findViewById(R.id.im_back);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        etMostEducation = (EditText) findViewById(R.id.et_mostEducation);
        etEducation = (EditText) findViewById(R.id.et_education);
        etAddress = (EditText) findViewById(R.id.et_address);
        etExperience = (EditText) findViewById(R.id.et_experience);
        etIndividualResume = (EditText) findViewById(R.id.et_individualResume);
        etPositionId = (EditText) findViewById(R.id.et_positionId);
        etMoney = (EditText) findViewById(R.id.et_money);
        btOk = (Button) findViewById(R.id.bt_ok);

        tvTitle.setText("编辑简历");

        if (queryResumeBean != null){
            //setText() 很容易因为给的是int类型的值闪退，反过来检查你还不知道哪里错了
            etMostEducation.setText(queryResumeBean.getMostEducation());
            etEducation.setText(queryResumeBean.getEducation());
            etAddress.setText(queryResumeBean.getAddress());
            etExperience.setText(queryResumeBean.getExperience());
            etIndividualResume.setText(queryResumeBean.getIndividualResume());
            etPositionId.setText(queryResumeBean.getPositionId()+"");
            etMoney.setText(queryResumeBean.getMoney());
        }
    }

    @Override
    public void initEvent() {
        imBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getThis().finish();
            }
        });

        btOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changResume();
            }
        });
    }

    private void changResume(){
        LoadedDialog dialog = new LoadedDialog(JobResumeActivity.this);
        dialog.show();
        JobResumeBean resumeBean = new JobResumeBean();
        resumeBean.setAddress(etAddress.getText().toString());
        resumeBean.setIndividualResume(etIndividualResume.getText().toString());
        resumeBean.setEducation(etEducation.getText().toString());
        resumeBean.setExperience(etExperience.getText().toString());
        resumeBean.setMoney(etMoney.getText().toString());
        resumeBean.setMostEducation(etMostEducation.getText().toString());
        //主键id 为必须参数 id由已存在的数据给出
        resumeBean.setId(queryResumeBean.getId());
        resumeBean.setPositionId(Integer.parseInt(etPositionId.getText().toString()));
        String gson = new Gson().toJson(resumeBean);
        //PUT 方法
        Api.config(ApiConfig.JOB_ADD_RESUME,null).putRequest(new NetCallBack() {
            @Override
            public void onSuccess(String res) {
                dialog.dismiss();
                if (res.contains("200")){
                    showToastSync("已修改");
                }else {
                    showToastSync(res);
                }
            }

            @Override
            public void onFailure(Exception e) {
                dialog.dismiss();
            }
        },JobResumeActivity.this,gson);
    }
}